package com.yjd.pub.fundservice.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.yjd.comm.annotation.Cacheable;
import com.yjd.comm.bank.model.BankModel;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.enums.StagesStatusEnum;
import com.yjd.comm.fundservice.enums.FundChannelEnum;
import com.yjd.comm.fundservice.util.QianKangServer;
import com.yjd.comm.order.model.OrderModel;
import com.yjd.comm.org.model.OrganizationModel;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.util.FrameUtil;
import com.yjd.pub.bank.dao.IBankDao;
import com.yjd.pub.order.dao.IOrderDao;
import com.yjd.pub.user.dao.IUserDataDao;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.fundservice.model.FundChannelModel;
import com.yjd.comm.fundservice.service.IFundChannelService;
import com.yjd.pub.fundservice.dao.IFundChannelDao;

/**
 * <b>description</b>：资金渠道业务实现<br>
 * <b>time</b>：2018-01-25 17:14:39 <br>
 * <b>author</b>：  yangchangyan@yijiedai.com
 */
@Service("fundChannelService")
public class FundChannelServiceImpl extends BaseServiceImpl implements IFundChannelService {

	@Resource
	private IFundChannelDao fundChannelDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.fundChannelDao;
	}

	@Resource
	private IOrganizationService organizationService;

	@Resource
	private IOrderDao orderDao;

	@Resource
	private IUserDataDao userDataDao;

	@Resource
	private IBankDao bankDao;

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public FundChannelModel insert(FundChannelModel model) throws Exception {
		return this.fundChannelDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(FundChannelModel model) throws Exception {
		return this.fundChannelDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param fund_channel
	 * @return
	 * @throws Exception
	 */
	public int delete(long fund_channel) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("fund_channel", fund_channel);
		return this.fundChannelDao.deleteModel(paramMap, true);
	}

	@Override
	public void loanApply(Long org_id, Long order_id) throws Exception {
		if (order_id == null) {
			FrameUtil.throwBaseException1("参数不能为空！");
		}
		OrderModel orderModel = orderDao.getModelById(order_id, true);
		if (orderModel == null) {
			FrameUtil.throwBaseException1("订单不存在");
		}

		if(orderModel.getFund_channel().intValue() < 1){
			FrameUtil.throwBaseException1("订单不支持资金渠道");
		}

		OrganizationModel organizationModel = organizationService.getModelById(org_id, DbWREnums.READ);
		if (organizationModel == null) {
			FrameUtil.throwBaseException1("当前机构不存在");
		}

		if (orderModel.getOrg_id().intValue() != organizationModel.getOrg_id().intValue()) {
			FrameUtil.throwBaseException1("当前订单不属于您的机构");
		}

		if (StagesStatusEnum.MERCHANT_HANDING.getCode() != orderModel.getStatus()) {
			FrameUtil.throwBaseException1("当前订单状态不可操作");
		}

		FundChannelModel fundChannel = this.fundChannelDao.getModelById(orderModel.getFund_channel(), false);
		if(null == fundChannel){
			FrameUtil.throwBaseException1("非法的资金渠道");
		}

		FundChannelEnum channel = FundChannelEnum.parse(fundChannel.getChannel_code());
		if(null == channel){
			FrameUtil.throwBaseException1("非法的资金渠道");
		}

		switch (channel){
			case QIAN_KANG:{
				UserDataModel userData = this.userDataDao.getModelById(orderModel.getUser_id(), false);
				BankModel bank = this.bankDao.getModelById(orderModel.getUser_id(), false);
				QianKangServer.loanApply(organizationModel, orderModel, userData, bank);
				break;
			}

			default:{ FrameUtil.throwBaseException1("此路不通"); }
		}

	}

	@Cacheable
    @Override
    public List<FundChannelModel> queryAllFundChannel() throws Exception {
        return fundChannelDao.getModelList(FrameUtil.newHashMap("fund_channel_ge", "0"), false);
    }
}
